import Cookies from 'js-cookie'
import {getFooterDetail} from "@/api/footer";
import {getFooter, setFooter} from "@/utils/auth";
import {Message} from "element-ui";
const footerData = getFooter();

var parsedFooterData = {}

if (footerData) {
  try {
    parsedFooterData = JSON.parse(footerData);
  } catch (error) {
    console.error('Error parsing footerData:', error);
  }
}
const state = {
  sidebar: {
    opened: Cookies.get('sidebarStatus') ? !!+Cookies.get('sidebarStatus') : true,
    withoutAnimation: false
  },
  device: 'desktop',
  activeMenu: "",
  footer:parsedFooterData,
}

const mutations = {
  TOGGLE_SIDEBAR: state => {
    state.sidebar.opened = !state.sidebar.opened
    state.sidebar.withoutAnimation = false
    if (state.sidebar.opened) {
      Cookies.set('sidebarStatus', 1)
    } else {
      Cookies.set('sidebarStatus', 0)
    }
  },
  CLOSE_SIDEBAR: (state, withoutAnimation) => {
    Cookies.set('sidebarStatus', 0)
    state.sidebar.opened = false
    state.sidebar.withoutAnimation = withoutAnimation
  },
  TOGGLE_DEVICE: (state, device) => {
    state.device = device
  },
  CHANGE_ACTIVEMENU: (state, activeMenu) => {
    state.activeMenu = activeMenu
  },
  SET_FOOTER: (state, footer) => {
    state.footer = footer;
  },
};

const actions = {
  toggleSideBar({commit}) {
    commit('TOGGLE_SIDEBAR')
  },
  closeSideBar({commit}, {withoutAnimation}) {
    commit('CLOSE_SIDEBAR', withoutAnimation)
  },
  toggleDevice({commit}, device) {
    commit('TOGGLE_DEVICE', device)
  },
  changeActiveMenu({commit}, path) {
    commit('CHANGE_ACTIVEMENU', path);
  },
  getFooter({commit}) {
    return new Promise((resolve, reject) => {
      getFooterDetail().then((response) => {
        var  {data} = response;
        // data.webTemplateType = "zhuanjiV4_blue_gx"
        if (data.customerPhones ) {
          data.customerPhones = data.customerPhones.split('|');
        }
        commit('SET_FOOTER', data);
        setFooter(JSON.stringify(data));
        resolve()
      }).catch((error) => {
        Message.error(error.message);
        reject(error)
      });
    });
  },
  /*setAppid({commit},appid){
    commit('SET_APPID', appid);
    setUuid(appid);
  },*/
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
